package edu.gyc.hiwxm.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    /**
     * 获取固定间隔时刻集合
     * @param start 开始时间
     * @param end 结束时间
     * @param interval 时间间隔(单位：分钟)
     * @return
     */
    public static List<String> getIntervalTimeList(String start, String end, int interval){
        Date startDate = convertString2Date("yyyy-MM-dd HH:mm",start);
        Date endDate = convertString2Date("yyyy-MM-dd HH:mm",end);
        List<String> list = new ArrayList<>();
        while(startDate.getTime()<=endDate.getTime()){
            list.add(convertDate2String("yyyy-MM-dd HH:mm",startDate));
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            calendar.add(Calendar.MINUTE,interval);
            if(calendar.getTime().getTime()>endDate.getTime()){
                if(!startDate.equals(endDate)){
                    list.add(convertDate2String("yyyy-MM-dd HH:mm",endDate));
                }
                startDate = calendar.getTime();
            }else{
                startDate = calendar.getTime();
            }

        }
        return list;
    }
    public static Date convertString2Date(String format, String dateStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            Date date = simpleDateFormat.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static String convertDate2String(String format,Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }

    /**
     *
     * @param start
     * @param end
     * @param interval
     * @return 排班时间表工具类
     *
     *
     *
     *
     *
     *
     */
    public static List<String > getTimeBlockList(String start, String end, int interval){
        List<String> list = getIntervalTimeList(start, end, interval);
        List<String > result = new ArrayList<>();
        result.add(list.get(0)+"-"+list.get(1));
        for (int i = 1; i < list.size()-1; i++) {
            if(i+1>list.size()) break;
            result.add(list.get(i)+"-"+list.get(i+1));
        }
        return result ;
    }
    public static void main(String[] args) {
        // List<String> list = getTimeBlockList("2020-01-10 09:30:00", "2020-01-10 24:00:00", 20);

        //  System.out.println(list);
        Date date=  convertString2Date("yyyy-MM-dd HH:mm:ss", "2020-01-10 10:10:00");
        System.out.println(date.toString());
       /*
        List<String > result = new ArrayList<>();
        List<Integer> ints = new ArrayList<>();
        int size = list.size();
        int flag = 2;
        int temp = size / flag + 1;
        boolean special = size % flag == 0;
        List<List> cutList = new ArrayList<>();
        for (int i = 0; i < temp; i++) {
            if (i == temp - 1) {
                if (special) {
                    break;
                }
                cutList .add(list.subList(flag * i, size)) ;
            } else {
                cutList.add(list.subList(flag * i, flag * (i + 1)) );
            }


        }

        System.out.println( cutList.toString());

        for (List l : cutList) {
            StringBuffer buffer = new StringBuffer();
           String b= (String) l.get(0);
           if(l.size()==2){
               String e = (String) l.get(1);
               buffer.append(b);
               buffer.append("-");
               buffer.append(e);
               result.add(buffer.toString());
           }

        }
        System.out.println(result);

        */
    }


}
